{% if payment_pp_express_incontext_disable != 1 %}
<div class="buttons">
  <div id="button-confirms" class="buttonFloat" style="display: block;    position: absolute;
    right: 0;
    bottom: 0;
    height: 91px;
    width: 252px;
    z-index: 2;"></div>
  <div class="pull-right">
    <a id="button-confirm" href="{{ continue }}"></a>
  </div>
</div>
	<div class="flaot-pay hidden-lg hidden-md hidden-sm">
		<div class="price">{{ '$43.95' }}</div>
		<input type="button" value="{{ button_confirm }}" id="button-confirm-mobile" data-loading-text="{{ text_loading }}" class="blue-btn-confirm" />
	</div>
<script type="text/javascript"><!--
//$('#button-confirms').on('click', function() {
{% if not logged %}
document.querySelector('#button-confirms').addEventListener('click', function(event) {
    $.ajax({
        url: 'index.php?route=checkout/guest/savehebing',
        type: 'post',
        data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address textarea, #collapse-payment-address select'),
        dataType: 'json',
        async:false,
        beforeSend: function () {
            $("#button-confirm").attr('disabled',true);
        },
        success: function(json) {
            
            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                var elementnoteo='';
				for (i in json['error']) {
					var element = $('#input-payment-' + i.replace('_', '-'));
                    if(!elementnoteo){
					   elementnoteo=element;
					}
					if ($(element).parent().hasClass('input-group')) {
						$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
					} else {
						$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
					}
				}
				//console.log(element.attr("id"));
                window.location.hash = "#"+elementnoteo.attr("id");
				// Highlight any found errors
				$('.text-danger').parent().addClass('has-error');
            } else {
                
                var shoppingsameaddress = $('#collapse-payment-address input[name=\'shoppingsameaddress\']:checked').val();
				if(shoppingsameaddress=='shoppingdifferent'){
				    savegshoping();  
				}else{
				   savegpaddess();
				}
                 
                
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        },
        complete: function () {
            $("#button-confirm").attr('disabled',false);
        }

    });
	
});
//浮块
document.querySelector('#button-confirm-mobile').addEventListener('click', function(event) {
    $.ajax({
        url: 'index.php?route=checkout/guest/savehebing',
        type: 'post',
        data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address textarea, #collapse-payment-address select'),
        dataType: 'json',
        async:false,
        beforeSend: function() {
            $("#button-confirm-mobile").attr('disabled',true);
        },
        complete: function() {
            $("#button-confirm-mobile").attr('disabled',false);
        },
        success: function(json) {
            
            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                var elementnoteo='';
				for (i in json['error']) {
					var element = $('#input-payment-' + i.replace('_', '-'));
                    if(!elementnoteo){
					   elementnoteo=element;
					}
					if ($(element).parent().hasClass('input-group')) {
						$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
					} else {
						$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
					}
				}
				//console.log(element.attr("id"));
                window.location.hash = "#"+elementnoteo.attr("id");
				// Highlight any found errors
				$('.text-danger').parent().addClass('has-error');
            } else {
                
                var shoppingsameaddress = $('#collapse-payment-address input[name=\'shoppingsameaddress\']:checked').val();
				if(shoppingsameaddress=='shoppingdifferent'){
				    savegshoping();  
				}else{
				   savegpaddess();
				}
                 
                
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
	
});
{% else %}
document.querySelector('#button-confirms').addEventListener('click', function(event) {
    $.ajax({
		url: 'index.php?route=checkout/payment_address/savehebingboth',
		type: 'post',
		data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'password\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address textarea, #collapse-payment-address select,#collapse-shipping-address input[type=\'text\'], #collapse-shipping-address input[type=\'date\'], #collapse-shipping-address input[type=\'datetime-local\'], #collapse-shipping-address input[type=\'time\'], #collapse-shipping-address input[type=\'password\'], #collapse-shipping-address input[type=\'checkbox\']:checked, #collapse-shipping-address input[type=\'radio\']:checked, #collapse-shipping-address textarea, #collapse-shipping-address select'),
		dataType: 'json',
		beforeSend: function() {
            $("#button-confirm").attr('disabled',true);
		},
		complete: function() {
            $("#button-confirm").attr('disabled',false);
		},
		async:false, 
		success: function(json) {
			$('.alert-dismissible, .text-danger').remove();
			$('.form-group').removeClass('has-error');

			if (json['redirect']) {
				location = json['redirect'];
			} else if (json['error']) {
				if (json['error']['warning']) {
					$('#collapse-payment-address .panel-body').prepend('<div class="alert alert-warning alert-dismissible">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
				}
                var elementnoteo='';
				for (i in json['error']) {
					var element = $('#input-payment-' + i.replace('_', '-'));
                    if(!elementnoteo){
					   elementnoteo=element;
					}
					if ($(element).parent().hasClass('input-group')) {
						$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
					} else {
						$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
					}
				}
                window.location.hash = "#"+elementnoteo.attr("id");
				
				var elementnotes='';
				for (i in json['error']) {
				console.log(i);
					var element = $('#input-shipping-' + i.replace('_', '-'));
                    if(!elementnotes){
					   elementnotes=element;
					}
					if ($(element).parent().hasClass('input-group')) {
						$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
					} else {
						$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
					}
				}
                window.location.hash = "#"+elementnotes.attr("id");
				// Highlight any found errors
				$('.text-danger').parent().parent().addClass('has-error');
			} else {
				   
				///////
				savegpaddess();
				/////// 
				   
				   
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
	
});
//浮块
document.querySelector('#button-confirm-mobile').addEventListener('click', function(event) {
    $.ajax({
		url: 'index.php?route=checkout/payment_address/savehebingboth',
		type: 'post',
		data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'password\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address textarea, #collapse-payment-address select,#collapse-shipping-address input[type=\'text\'], #collapse-shipping-address input[type=\'date\'], #collapse-shipping-address input[type=\'datetime-local\'], #collapse-shipping-address input[type=\'time\'], #collapse-shipping-address input[type=\'password\'], #collapse-shipping-address input[type=\'checkbox\']:checked, #collapse-shipping-address input[type=\'radio\']:checked, #collapse-shipping-address textarea, #collapse-shipping-address select'),
		dataType: 'json',
        beforeSend: function() {
            $("#button-confirm-mobile").attr('disabled',true);
        },
        complete: function() {
            $("#button-confirm-mobile").attr('disabled',false);
        },
		async:false, 
		success: function(json) {
			$('.alert-dismissible, .text-danger').remove();
			$('.form-group').removeClass('has-error');

			if (json['redirect']) {
				location = json['redirect'];
			} else if (json['error']) {
				if (json['error']['warning']) {
					$('#collapse-payment-address .panel-body').prepend('<div class="alert alert-warning alert-dismissible">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
				}
                var elementnoteo='';
				for (i in json['error']) {
					var element = $('#input-payment-' + i.replace('_', '-'));
                    if(!elementnoteo){
					   elementnoteo=element;
					}
					if ($(element).parent().hasClass('input-group')) {
						$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
					} else {
						$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
					}
				}
                window.location.hash = "#"+elementnoteo.attr("id");
				
				var elementnotes='';
				for (i in json['error']) {
				console.log(i);
					var element = $('#input-shipping-' + i.replace('_', '-'));
                    if(!elementnotes){
					   elementnotes=element;
					}
					if ($(element).parent().hasClass('input-group')) {
						$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
					} else {
						$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
					}
				}
                window.location.hash = "#"+elementnotes.attr("id");
				// Highlight any found errors
				$('.text-danger').parent().parent().addClass('has-error');
			} else {
				   
				///////
				savegpaddess();
				/////// 
				   
				   
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
	
});
{% endif %}

function savegpaddess(){
    ///////
							$.ajax({
								url: 'index.php?route=checkout/confirm_he',
								dataType: 'html',
								async:false, 
								success: function(html) {
									
									var e = document.createEvent("MouseEvents");
									 e.initEvent("click", true, true);
									document.getElementById("button-confirm").dispatchEvent(e);
							   
									
									//window.location = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-25T76600JU658143L&useraction=commit";

								},
								error: function(xhr, ajaxOptions, thrownError) {
									alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
								}
							});
							///////
}
function savegshoping(){
    $.ajax({
        url: 'index.php?route=checkout/guest_shipping/savehebing',
        type: 'post',
        data: $('#collapse-shipping-address input[type=\'text\'], #collapse-shipping-address input[type=\'date\'], #collapse-shipping-address input[type=\'datetime-local\'], #collapse-shipping-address input[type=\'time\'], #collapse-shipping-address input[type=\'password\'], #collapse-shipping-address input[type=\'checkbox\']:checked, #collapse-shipping-address input[type=\'radio\']:checked, #collapse-shipping-address textarea, #collapse-shipping-address select'),
        dataType: 'json',
        beforeSend: function() {
            $("#button-confirm-mobile").attr('disabled',true);
        },
        complete: function() {
            $("#button-confirm-mobile").attr('disabled',false);
        },
		async:false,
        success: function(json) {
            $('.alert-dismissible, .text-danger').remove();
			$('.form-group').removeClass('has-error');

            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                $('#button-guest-shipping').button('reset');

                if (json['error']['warning']) {
                    $('#collapse-shipping-address .panel-body').prepend('<div class="alert alert-danger alert-dismissible">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                }
                var elementnote='';
				for (i in json['error']) {
					var element = $('#input-shipping-' + i.replace('_', '-'));
                    if(!elementnote){
					   elementnote=element;
					}
					if ($(element).parent().hasClass('input-group')) {
						$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
					} else {
						$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
					}
				}
                window.location.hash = "#"+elementnote.attr("id");
				// Highlight any found errors
				$('.text-danger').parent().addClass('has-error');
            } else {
			    savegpaddess();
                
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });


}




window.paypalCheckoutReady = function () {
  paypal.checkout.setup('{{ username }}', {
    container: 'button-confirm',
    environment: 'sandbox'
  });
};
//--></script>
<script src="//www.paypalobjects.com/api/checkout.js" async></script>

{% else %}
<div class="buttons">
  <div class="pull-right">
    <a href="{{ continue }}" class="btn btn-primary" id="button-confirm" data-loading-text="{{ text_loading }}">{{ button_continue }}</a>
  </div>
</div>
<script type="text/javascript"><!--
    $('#button-confirm').on('click', function() {
        $('#button-confirm').button('loading');
    });
//--></script>
{% endif %}
